// pages/carover/carover.js
var api = require('../../api.js');
var seti=null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderinfo:null,
    minute:0,
    second:0,
    overtime:0,
    showView: true
  },
  UserPopup: function(){
    var that = this;
    that.setData({
      showView: (!that.data.showView)
    })
  },
  calling: function () {
    wx.makePhoneCall({ 
      phoneNumber: '4000571118',
      success:function(){
        console.log("拨打电话成功！") 
      },
      fail:function(){
        console.log("拨打电话失败！")
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    wx.request({
      url: api.orderinfo,
      method: 'POST',
      data: {
        id: options.orderid,
      },
      success: function (res) {
        console.log(res.data);
        that.setData({
          orderinfo: res.data,
        })
        var overtime = 0;
        seti = setInterval(function () {
          var timestamp = Date.parse(new Date()) / 1000;
          overtime = res.data.payovertime - timestamp;
          console.log(overtime);
          if (overtime <= 0) {
            clearInterval(seti);
            return false;
          }
          that.setData({
            overtime: overtime,
            minute: Math.floor(overtime / 60),
            second: overtime % 60,
          })
        }, 1000)

      },
      fail: function (data) {
        console.log("orderinfo" + data);
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },
  pay:function(e){
    var that=this;
    var userinfo = wx.getStorageSync('userinfo');
    var bu_price=9999
    if (userinfo.money - that.data.orderinfo.price<0){
      bu_price = (that.data.orderinfo.price - userinfo.money).toFixed(2);
    }else{
      wx.showToast({
        title: '余额已经充足, 不支持补差价下单,请重新下单',
        icon: "none",
        duration: 2000
      })
      return false;
    }
    wx.setStorage({
      key: 'orderInfo',
      data: {
        departure: that.data.orderinfo.departure,
        destination: that.data.orderinfo.destination,
        usetime: that.data.orderinfo.usetime,
        openid: that.data.orderinfo.openid,
        cartype: that.data.orderinfo.cartype,
        price: that.data.orderinfo.price,
        pay_type: that.data.orderinfo.pay_type,
        bu_price: bu_price,
        flight_id: that.data.orderinfo.flight_id,
        isticket: that.data.orderinfo.isticket,
        producttype: that.data.orderinfo.producttype,
        distance: that.data.orderinfo.distance,
      },
    })
    wx.redirectTo({
      url: '../carpay/carpay?id=' + e.target.dataset.id,
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(seti);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})